//index.js
//获取应用实例 
var app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    token:'',
    accountId:'',
    codeId: '',
    passId: '',
    paddPassId: '',
    inviteId: '',
    inviteCode:'',
    code: '',//验证码
    iscode: null,//用于存放验证码接口里获取到的code
    codename: '获取验证码',
    temp:''
  },
  onShow:function(){
    
  },
  onLoad: function () {
    this.setData({
      accountId: wx.getStorageSync("account"),
      passId: wx.getStorageSync("pwd")

    })
  },
 
  accountInput: function (e) {
    var that = this;
    that.setData({
      accountId: e.detail.value
    })
  },
  passwordInput: function (e) {
    var that = this;
    that.setData({
      passId: e.detail.value
    })
  },
  onReady: function () {
   
  },
  toregister:function(){
    wx.navigateTo({
      url: 'register',
    })
  },
  upPassword:function(){
    wx.navigateTo({
      url: 'up-password',
    })
  },
  //denglu
  login: function () {
    var that=this;
    if (that.data.accountId.length==0){
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    if (that.data.passId.length == 0) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return;
    }
    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=login',
      data:
       {
        account: ''+that.data.accountId,//手机号码
        pwd: ''+that.data.passId,//密码
        platform:'miniwx'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'JSON',
      success: function (res) {
        console.log(res)
        var jsonRes=JSON.parse(res.data)
        if (jsonRes.code==0){
          //缓存token
          wx.setStorageSync('token', jsonRes.token); //将userIdEnc存入本地缓存
          wx.setStorageSync('uid', jsonRes.uid); //将userIdEnc存入本地缓存
          wx.setStorageSync('account',that.data.accountId); //将userIdEnc存入本地缓存
          wx.setStorageSync('pwd', that.data.passId); //将userIdEnc存入本地缓存
          wx.showToast({
            title: '登录成功',//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
          app.globalData.token=jsonRes.token,
          app.globalData.uid=jsonRes.uid,
          app.globalData.group_id=jsonRes.group_id
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/live/home',
            })
          }, 1000) //延迟时间 这里是1秒
        }else{
          wx.showToast({
            title: '' + jsonRes.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
      }, fail: function () {

      }
    });
  },
  // login:function(){
  //   
  // },
})
